package com.warmlight.voicepacket.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.RecommendData;
import com.warmlight.voicepacket.service.ADService;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.ToastUtil;
import com.warmlight.voicepacket.web.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现页面的banner adapter
 * Created by jason on 2018/5/23.
 */

public class TopicBannerPagerAdapter extends PagerAdapter {
    private List<RecommendData.DataBean.BannerBean> mList;
    private Context mContext;

//    private List<String> mImageData;

    public TopicBannerPagerAdapter(Context context, List<RecommendData.DataBean.BannerBean> list) {
        mList = list;
        mContext = context;
//        initImageData();
    }

//    private void initImageData(){
//        mImageData = new ArrayList<>();
//        mImageData.add(TestData.mImagePath);
//        mImageData.add(TestData.mImagePath2);
//        mImageData.add(TestData.mImagePath3);
//        mImageData.add(TestData.mImagePath4);
//        mImageData.add(TestData.mImagePath5);
//        mImageData.add(TestData.mImagePath6);
//    }

    @Override
    public int getCount() {
//        return 6;
        return mList == null ? 0 : Integer.MAX_VALUE;
//        return mList == null ? 0 : mList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(View container, final int position) {
        ImageView iv = (ImageView) View.inflate(mContext, R.layout.item_image_banner_topic, null);
        final RecommendData.DataBean.BannerBean bannerBean = mList.get(position % mList.size());
//        GlideRN.loadImage(mContext, mImageData.get(position));
        if(mContext!=null){
            if (!((Activity)mContext).isFinishing()) {
                try {
                    RequestOptions options = new RequestOptions();
                    options.placeholder(R.drawable.icon_banner_placeholder).dontAnimate();
                    Glide.with(mContext).load(bannerBean.getPic()).apply(options).into(iv);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        //TODO 根据业务，添加点击事件，根据不同type添加跳转事件，携带参数。
        final String type = bannerBean.getType();
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext,mImageData.get(position).toString(),Toast.LENGTH_SHORT).show();
                MobclickAgent.onEvent(mContext,"dis_banner_click",bannerBean.getValue());
                ClickLog.clickLog("dis_banner_click","0",bannerBean.getValue());
                if ("1".equals(type)){
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra("url",bannerBean.getValue());
                    mContext.startActivity(intent);
                }else if (TextUtils.equals("3",type)){
                    Intent intent = new Intent();
                    intent.setData(Uri.parse(bannerBean.getValue()));//Url 就是你要打开的网址
                    intent.setAction(Intent.ACTION_VIEW);
                    mContext.startActivity(intent); //启动浏览器
                }else if (TextUtils.equals("4",type)){
                    Intent intent = new Intent(mContext, ADService.class);
                    intent.putExtra("apk",bannerBean.getValue());
                    mContext.startService(intent);
                    ToastUtil.showShort(mContext,"开始下载应用");
                }
            }
        });
        ((ViewPager) container).addView(iv);
        return iv;
    }

    public void setList(List<RecommendData.DataBean.BannerBean> list) {
        if (mList == null) {
            mList = new ArrayList<RecommendData.DataBean.BannerBean>();
        }
        mList = list;
        notifyDataSetChanged();
    }

    public void addList(List<RecommendData.DataBean.BannerBean> list) {
        if (mList == null) {
            mList = new ArrayList<RecommendData.DataBean.BannerBean>();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearList() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }
}
